import { kilom } from "kilom";
import msgCfg from "./cfg/msgcfg";
import { svrCfg } from "./cfg/svrcfg";


export const app = kilom.createApp({
    svrCfg,
    msgCfg
});

app.miscCfg.logger = (level, msg) => {
    console.log(msg);
}
app.miscCfg.autoReloadHandler = true;

app.miscCfg.reloadMsgCfgPath = "./cfg/msgcfg";

app.miscCfg.onStop = async () => {
    if (app.svr.type == "connector") {
        app.logger.info("两秒后关闭服务器");
        await new Promise(res => {
            setTimeout(res, 2000);
        })
    }
}

app.setSvrRouter("connector", (sess, svrCfgs) => {
    //随机一个connector服务器提供客户端连接
    return svrCfgs[Math.floor(Math.random() * svrCfgs.length)];
})

app.miscCfg.onCmd = cmd => {
    app.logger.debug(cmd);
    return { code: 0 };
}


app.start();

if (app.svr.type == "connector") {

    app.miscCfg.onClientClose = (sess) => {
        if (sess.uid) {
            app.rpc("game").game.main.leave(sess.uid);
        }
    }
}